# L = []
# n = 1
# while n <= 99:
#     L.append(n)
#     n = n + 2
# print(L)




# #利用切片操作，实现一个trim函数，将字符串收尾的空格去掉

# # #自己写的 仅切片一次，并没有好好练习切片的特性
# # def trim(s):
# #     i = 0
# #     n = len(s) - 1
# #     while i <= n and s[i] == ' ':#需要保证i <= n
# #         i = i + 1
# #     while n >= 0 and s[n] == ' ':#需要保证n >= 0
# #         n = n - 1
# #     s = s[i:n+1]
# #     return s

# #参照评论区的写法，多次切片
# def trim(s):
#     while len(s) > 0:
#         if s[0] == ' ':
#             s = s[1:]#从s[1]到末尾切片即去掉s[0]
#         elif s[-1] == ' ':
#             s = s[:-1]#从开头至s[-1]但是不包括s[-1]
#         else:
#             break#若前后都不是空格 则说明已完成空格的去除，结束循环
#     return s


# # 测试:
# if trim('hello  ') != 'hello':
#     print('测试失败!')
# elif trim('  hello') != 'hello':
#     print('测试失败!')
# elif trim('  hello  ') != 'hello':
#     print('测试失败!')
# elif trim('  hello  world  ') != 'hello  world':
#     print('测试失败!')
# elif trim('') != '':
#     print('测试失败!')
# elif trim('    ') != '':
#     print('测试失败!')
# else:
#     print('测试成功!')



# #请使用迭代查找一个list中最小和最大值，并返回一个tuple：
# def findMinAndMax(L):
#     if len(L) == 0:
#         return (None, None)
#     min = L[0]
#     max = L[0]
#     for i in L:
#         if i > max:
#             max = i
#         elif i < min:
#             min = i
#     return (min,max)

# # 测试
# if findMinAndMax([]) != (None, None):
#     print('测试失败!')
# elif findMinAndMax([7]) != (7, 7):
#     print('测试失败!')
# elif findMinAndMax([7, 1]) != (1, 7):
#     print('测试失败!')
# elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
#     print('测试失败!')
# else:
#     print('测试成功!')



# L1 = [x if x % 2 == 0 else -x for x in range(1, 11)]
# print(L1)
# L2 = [x for x in range(1, 11) if x % 2 == 0]
# print(L2)



# L1 = ['Hello', 'World', 18, 'Apple', None]
# L2 = [s.lower() for s in L1 if isinstance(s,str)]

# # 测试:
# print(L2)
# if L2 == ['hello', 'world', 'apple']:
#     print('测试通过!')
# else:
#     print('测试失败!')



# #斐波那契数列Fibonacci 将函数形式改为生成器函数形式
# def fib(max):
#     n , a , b = 0 , 0 , 1
#     while n < max:
#         #print(b)  只需将 print(b) 改为 yield(b) 即可
#         yield(b)
#         a , b = b ,a + b
#         n = n + 1
#     return 'done'
# for n in fib(6):
#     print(n)

#     #若想得到return的值 必须捕获 StopIteration 错误
# g = fib(6)
# while True:
#     try:
#         x = next(g)
#         print('g:',x)
#     except StopIteration as e:
#         print('Generator return value:',e.value)
#         break




#杨辉三角 将每行看作一个list 试写一个generator 不断输出下一行的list

def triangles():

    #高效写法一
    # L = [1]
    # n = 0
    # while n < 10:
    #     yield L
    #     L = [1] + [L[i] +L[i+1] for i in range(len(L)-1)] + [1]
    # return 'done'

    #高效写法二
    row =[1] 
    while True: 
        yield row 
        row=[x+y for x,y in zip([0]+row,row+[0])]  #无敌的写法 666


# 期待输出:
# [1]
# [1, 1]
# [1, 2, 1]
# [1, 3, 3, 1]
# [1, 4, 6, 4, 1]
# [1, 5, 10, 10, 5, 1]
# [1, 6, 15, 20, 15, 6, 1]
# [1, 7, 21, 35, 35, 21, 7, 1]
# [1, 8, 28, 56, 70, 56, 28, 8, 1]
# [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
n = 0
results = []
for t in triangles():
    results.append(t)
    n = n + 1
    if n == 10:
        break

for t in results:
    print(t)

if results == [
    [1],
    [1, 1],
    [1, 2, 1],
    [1, 3, 3, 1],
    [1, 4, 6, 4, 1],
    [1, 5, 10, 10, 5, 1],
    [1, 6, 15, 20, 15, 6, 1],
    [1, 7, 21, 35, 35, 21, 7, 1],
    [1, 8, 28, 56, 70, 56, 28, 8, 1],
    [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
]:
    print('测试通过!')
else:
    print('测试失败!')
